import Tpl from './index.ejs';
import DistributorAddTpl from './distributorAdd.ejs';
import DistributorListTpl from './distributorList.ejs';
import SupplierAddTpl from './supplierAdd.ejs';
import SupplierListTpl from './supplierList.ejs';

import Tools from '../tools/tools.js';

class Account {

	init ($wrap, router) {
		let self = this;

		this.render($wrap, router, function () {

			$('.ui.checkbox').checkbox();
	        $('.dropdown').dropdown();

			self.$listWrap = $('#listWrap');
			self.reqData = {};

			self.bindEvent();
		});
	}

	bindEvent () {
		let self = this;

		$('#distributorAddBtn').on('click', function () {
			self.distributorAddForm();
		});

		$('#supplierAddBtn').on('click', function () {
			self.supplierAddFrom();
		});
	}

	distributorAddForm () {
		let self = this;

		self.reqData = $.extend({
			paged: true,
        	pageNo: 1,
        	pageSize: 10,
			type: 1
        }, Tools.getFromData());

        $.ajax({
			url: '/admin/distributor/account/add',
			method: 'post',
			data: JSON.stringify(self.reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					self.$listWrap.html(DistributorListTpl(data));
				}
			}
        });
	}

    supplierAddFrom () {
		let self = this;

		self.reqData = $.extend({
			paged: true,
        	pageNo: 1,
        	pageSize: 10,
			type: 1
        }, Tools.getFromData());

        $.ajax({
			url: '/admin/supplier/account/add',
			method: 'post',
			data: JSON.stringify(self.reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					self.$listWrap.html(SupplierListTpl(data));
				}
			}
        });
	}

	render ($wrap, router, callback) {
		if (!router) {
            $wrap.html(Tpl());
        }
        else {
            switch (router) {
                case 'distributor':

					$.ajax({
						url: '/admin/distributor/account/list',
						method: 'post',
						data: JSON.stringify({
							paged: true,
							pageNo: 1,
							pageSize: 10
						}),
						contentType: 'application/json',
						dataType: 'json',
						success: function (data) {

							if (data && data.message === 'SUCCESS') {

								$wrap.html(DistributorListTpl(data));
								callback();
							}
						}
					});

               		break;
               	case 'distributorAdd':

                    $wrap.html(DistributorAddTpl());
                    callback();
               		break;
                case 'supplier':

                    $.ajax({
                        url: '/admin/supplier/account/list',
                        method: 'post',
                        data: JSON.stringify({
                            paged: true,
                            pageNo: 1,
                            pageSize: 10
                        }),
                        contentType: 'application/json',
                        dataType: 'json',
                        success: function (data) {

                            if (data && data.message === 'SUCCESS') {

                                $wrap.html(SupplierListTpl(data));
                                callback();
                            }
                        }
                    });

                    break;
                case 'supplierAdd':

                    $.ajax({
                        url: '/city/info/list',
                        method: 'post',
                        data: JSON.stringify({
                            paged: true,
                            pageNo: 1,
                            pageSize: 10
                        }),
                        contentType: 'application/json',
                        dataType: 'json',
                        success: function (data) {

                            if (data && data.message === 'SUCCESS') {

                                $wrap.html(SupplierAddTpl(data));
                                callback();
                            }
                        }
                    });

                    break;
               	default:
               		break;
            }
        }
	}
}

export default new Account();
